package com.q.tqb.activity.mine;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.q.tqb.R;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.model.ShareInfo;
import com.q.tqb.util.SPUtil;

import com.q.tqb.view.popwindow.SharePopWindow;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class MineBusinessActivity extends BaseActivity {

    @BindView(R.id.iv_code_bg)
    ImageView mIvCodeBg;

    private Bitmap mBgBitmap;
    private Bitmap mBitmap;
    private String headUrl;
    private int uid;
    private boolean isShare;

    @Override
    public int getLayoutId() {
        return R.layout.activity_mine_business;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        headUrl = SPUtil.getString(MineBusinessActivity.this, "header");
        uid = (int) SPUtil.get(MineBusinessActivity.this, "uid", 0);
        setTitle(getString(R.string.mine_store));
        setTvRight(getString(R.string.string_share));
        loadImageSimpleTarget();
    }


    @OnClick({R.id.iv_back, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right:
                if (isShare) {
                    share();
                } else {
                    showToast("二维码图片未准备好！");
                }
                break;
        }
    }

    private SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>(250, 250) {
        @Override
        public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
            //这里我们拿到回掉回来的bitmap，可以加载到我们想使用到的地方
            mBgBitmap = ((BitmapDrawable) mIvCodeBg.getDrawable()).getBitmap();
            mBitmap = CodeUtils.createImage(AppConst.URL_CODE_SHARE + uid, 600, 600, bitmap);
            mBgBitmap = createWaterMaskCenter(mBgBitmap, mBitmap);
            mIvCodeBg.setImageBitmap(mBgBitmap);
            isShare = true;
        }

    };

    private void loadImageSimpleTarget() {
        Glide
                .with(MineBusinessActivity.this)
                .load(headUrl)
                .asBitmap()
                .into(target);
    }


    public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
        return createWaterMaskBitmap(src, watermark,
                (src.getWidth() - watermark.getWidth()) / 2,
                (src.getHeight() - watermark.getHeight()) / 2);
    }

    private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark,
                                                int paddingLeft, int paddingTop) {
        if (src == null) {
            return null;
        }
        int width = src.getWidth();
        int height = src.getHeight();
        // 创建一个新的和SRC长度宽度一样的位图
        Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //将该图片作为画布
        Canvas canvas = new Canvas(newb);
        //在画布 0，0坐标上开始绘制原始图片
        canvas.drawBitmap(src, 0, 0, null);
        //在画布上绘制水印图片
        canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
        // 保存
        canvas.save(Canvas.ALL_SAVE_FLAG);
        // 存储
        canvas.restore();
        return newb;
    }


    private void share() {
        ShareInfo shareInfo = new ShareInfo();
        shareInfo.setType(1);
        SharePopWindow sharePopWindow = new SharePopWindow(MineBusinessActivity.this, shareInfo, mBgBitmap);
        sharePopWindow.showAtLocation(findViewById(R.id.rl_main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }


}
